'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 建筑
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var soldierAttr = require('../libs/war/soldierAttr.js');
var soldierConf = require('../config/soldierConf.js');
var fightPowerService = require('./fightPowerService.js');
var buildType = require('../consts/buildType');

/**
 * 更新士兵战力
 * @param  {[type]}   model_list      [description]
 * @return {[type]}             [description]
 */
exports.updateSoldierPower = function (player_manager, update_single) {
    var self = this;
    var soldier_model = player_manager.soldier;
    var soldier_power = 0;
    var select_soldier_power = 0;
    var map_select_soldier = {};

    _.each(soldier_model.select_soldier_list, function (soldier_id) {
        map_select_soldier[soldier_id] = 1;
    });

    _.each(soldier_model.soldier_list, function (si) {
        var curr_power = self.getSoldierPower(si.soldier_type, si.lv, si.star, si.talent);
        soldier_power = soldier_power + curr_power;
        if (!!map_select_soldier[si.soldier_type]) {
            select_soldier_power = select_soldier_power + curr_power;
        }
    });

    soldier_model.soldier_power = Math.ceil(soldier_power);
    soldier_model.select_soldier_power = Math.ceil(select_soldier_power);

    if (!update_single) {
        // 刷新总战力
        fightPowerService.updByIdMainPower(soldier_model.role_id, player_manager, function (err) {
            if (!err) {
                player_manager.save();
            }
        });
    }
};

/**
 * 计算士兵战力
 * @param  {[type]}   soldier_type_list      [description]
 * @return {[type]}             [description]
 */
exports.calcSoldierPower = function (soldier_model, soldier_type_list) {
    var self = this;
    var select_soldier_power = 0;
    var map_select_soldier = {};

    _.each(soldier_type_list, function (soldier_type) {
        if (!!soldier_type) {
            map_select_soldier[soldier_type] = 1;
        }
    });

    _.each(soldier_model.soldier_list, function (si) {
        if (!!map_select_soldier[si.soldier_type]) {
            var curr_power = self.getSoldierPower(si.soldier_type, si.lv, si.star, si.talent);
            select_soldier_power += curr_power;
        }
    });

    select_soldier_power = Math.ceil(select_soldier_power);
    return select_soldier_power;
};

/**
 * 计算单个士兵战力
 * @param  {[type]}   model_list      [description]
 * @return {[type]}             [description]
 */
exports.getSoldierPower = function (soldier_type, lv, star, talent) {
    var soldier_type_conf = soldierConf.getSoldierTypeConf(soldier_type);
    var soldier_lv_conf = soldierConf.getSoldierLvConf(lv);
    var soldier_power = soldier_lv_conf.fp * Math.pow(1 + (star - 1) * 0.5, 1) * soldier_type_conf.fp_ratio;
    for (var x = 0; x < talent.length; x++) {
        var star_lv = x + 1;
        var talent_lv = talent[x] % 100;
        if (talent_lv > 0) {
            var talent_conf = soldierConf.getUpgradeTalentCostConf(star_lv, talent_lv);
            if (!talent_conf) {
                console.error('invalid talent_conf for %j %j', star_lv, talent_lv);
                continue;
            }
            soldier_power += talent_conf.fp;
        }
    }
    return soldier_power;
};

/**
 * 重算城墙士兵战力
 * @param  {[type]}   model_list      [description]
 * @return {[type]}             [description]
 */
exports.refreshWallSoldierPower = function (player_manager) {
    var soldier_model = player_manager.soldier;
    var build_model = player_manager.build;

    var wall_build = build_model.getBuildByType(buildType.WALL);
    if (!wall_build) {
        return;
    }

    var wall_soldier_list = wall_build.getSelectSoldier();
    var soldier_list = [];
    _.forEach(wall_soldier_list, function (batch_info) {
        soldier_list = _.union(soldier_list, batch_info.sub_soldier_list);
    });

    build_model.wall_soldier_power = this.calcSoldierPower(soldier_model, soldier_list);
};

/**
 * 按士兵类型列表，取出士兵相关信息
 */
exports.getSoldierInfoList = function (soldier_type_list, soldier_model) {
    var soldier_list = [];
    _.forEach(soldier_type_list, function (soldier_type) {
        if (soldier_type > 0) {
            var soldier_info = soldier_model.getSoldier(soldier_type);
            if (!!soldier_info) {
                soldier_list.push({
                    soldier_type: soldier_type,
                    soldier_lvl: soldier_info.lv,
                    soldier_star: soldier_info.star
                });
            } else {
                console.warn('invalid soldier_type:', soldier_type);
            }
        }
    });

    return soldier_list;
};
